<template>
  <div class="app-container">

   <!--查询-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="报告编号" prop="reportCode">
        <el-input
          v-model="queryParams.reportCode"
          placeholder="请输入报告编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="报告名称" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="请输入报告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crop:report:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crop:report:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crop:report:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crop:report:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--列表-->
    <el-table v-loading="loading" :data="checkReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报告名称" align="center" prop="reportName" />
      <el-table-column label="报告编号" align="center" prop="reportCode" />
      <el-table-column label="样品" align="center" prop="goodsName" />
      <el-table-column label="批次号" align="center" prop="materialCode" />
      <el-table-column label="检测日期" align="center" prop="detectDate" width="100"/>
      <el-table-column label="检验结果" align="center" prop="result">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="检验报告" align="center" prop="decidePic" >
        <template v-slot="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.decidePic">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crop:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crop:report:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检测报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">

        <el-form-item label="报告名称" prop="reportName">
          <el-input v-model="form.reportName" placeholder="请输入报告名称" />
        </el-form-item>

        <el-form-item label="样品" prop="goodsId">
          <el-select v-model="form.goodsId" placeholder="请选择样品">
            <el-option
              v-for="item in goodsList"
              :key="item.goodsId"
              :label="item.goodsName"
              :value="item.goodsId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="批次号" prop="materialCode">
          <el-select v-model="form.materialLotId" placeholder="请选择批次号">
            <el-option
              v-for="item in materialList"
              :key="item.materialCode"
              :label="item.materialCode"
              :value="item.materialCode">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="检测日期">
          <el-date-picker
            v-model="form.detectDate"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="检验单位" prop="detectUnit">
              <el-select v-model="form.detectUnit" placeholder="检验单位" clearable>
                <el-option
                  v-for="dict in dict.type.sys_unit"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验员" prop="detectName">
              <el-input v-model="form.detectName" placeholder="请输入检验员" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="审核人" prop="lister">
              <el-input v-model="form.lister" placeholder="请输入审核人" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="批准人" prop="auditor">
              <el-input v-model="form.auditor" placeholder="请输入批准人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="检验方法" prop="method">
              <el-input v-model="form.method" placeholder="请输入检验方法" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验设备" prop="equipment">
              <el-input v-model="form.equipment" placeholder="请输入检验设备" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="检验结果" prop="result">
              <el-select v-model="form.result" placeholder="检验结果" clearable>
                <el-option
                  v-for="dict in dict.type.sys_result"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验判定" prop="decide">
              <el-input v-model="form.decide" placeholder="请输入检验判定" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="检验报告" prop="decidePic">
          <el-upload
            class="upload-demo"
            action="http://localhost:9222/upload/uploadFile"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleAvatarSuccess"
            :file-list="fileList">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="imageUrl" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReport, getReport, delReport, addReport, updateReport, selectCheckReportCode} from "@/api/material/report";
import {listGoods} from "@/api/product/goods"
import {listMaterial} from "@/api/material/batch";

export default {
  name: "Report",
  dicts: ['sys_normal_disable','sys_crop_type','sys_crop_plant','sys_crop_workshop',
    'sys_fertilizer','sys_crop_status','sys_grow_status','sys_unit','sys_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农作物类别表格数据
      checkReportList: [],
      // 产物数据
      goodsList: [],
      //批次号数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportCode: undefined,
        reportName: undefined,
      },
      // 表单参数
      form: {
        num: 1,
      },
      // 表单校验
      rules: {
        farmerName: [
          { required: true, message: "农户名称不能为空", trigger: "blur" }
        ],
        person: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ]
      },
      //图片路径
      imageUrl: '',
      dialogVisible: false,
      //上传文件数组
      fileList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检测报告列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.checkReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询产物列表 */
    getGoodsList() {
      this.loading = true;
      listGoods().then(response => {
        this.goodsList = response.rows;
        //this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询批次号列表 */
    getMaterialList() {
      this.loading = true;
      listMaterial().then(response => {
        this.materialList = response.rows;
        //this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        reportId: undefined,
        reportName: undefined,
        reportCode: undefined,
        goodsId: undefined,
        materialCode: undefined,
        detectDate: undefined,
        detectUnit: undefined,
        detectName: undefined,
        lister: undefined,
        auditor: undefined,
        method: undefined,
        equipment: undefined,
        result: undefined,
        decide: undefined,
        decidePic: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getGoodsList();
      this.getMaterialList();
      this.open = true;
      this.title = "添加检测报告";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data;
        this.getGoodsList();
        this.getMaterialList();
        this.open = true;
        this.title = "修改检测报告";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reportId != undefined) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除检测报告编号为"' + reportIds + '"的数据项？').then(function() {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('crop/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },

    /** 文件上传 */
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
      this.form.decidePic=file;
    },
    handleAvatarSuccess(res, file, fileList) {
      this.form.decidePic="";
      for (let fileListElement of fileList) {
        var decidePic = fileListElement.response;
        console.log("图片"+decidePic);
        this.form.decidePic+=decidePic+",";
      }
      this.imageUrl = URL.createObjectURL(file.raw);

    },
  }
};
</script>
